#
# opentome_cbz.py
# (c) 2008 Will Baker.
#

from zipfile import ZipFile

def opentome_cbz_load_all_pages( loader, comic ):
    archive = ZipFile( comic.path )
    files = []
    for f in archive.namelist():
        if comic.is_supported_image_format( f ):
            files.append( f )
            
    loader.on_page_count_loaded( comic, len(files) )
    
    comic.sort_pages( files )
    page = 0
    for f in files:
        loader.on_page_data_loaded( comic, f, page, archive.read(f) )
        page += 1
            
    archive.close()
    
def opentome_cbz_load_single_page( loader, comic, page ):
    archive = ZipFile( comic.path )
    files = []
    for f in archive.namelist():
        if comic.is_supported_image_format( f ):
            files.append( f )
            
    loader.on_page_count_loaded( comic, len(files) )
            
    comic.sort_pages( files )
    loader.on_page_data_loaded( comic, files[page], page, archive.read(files[page]) )
    archive.close()
        
def opentome_register_plugin():
    return {
        'extensions'       : ['.cbz', '.zip'],
        'load_all_pages'   : opentome_cbz_load_all_pages,
        'load_single_page' : opentome_cbz_load_single_page,
        'pool'             : 3
    }
